Let's Build a Website Login Page with HTML, CSS, JavaScript and an External API

您所在的位置:网站首页 homepage with html and css Let's Build a Website Login Page with HTML, CSS, JavaScript and an External API

Let's Build a Website Login Page with HTML, CSS, JavaScript and an External API

2023-12-30 12:04| 来源: 网络整理| 查看: 265

image

This article will give you a basic tutorial for creating a website login page with HTML, CSS, JavaScript (Bootstrap 5) and an external API. The login will perform by the use of an external API from MeCallAPI.com (my website :D). If you want to try a mockup API for CRUD and authentication operations, feel free to check on the website.

https://www.mecallapi.com/

https://www.mecallapi.com/

You can also check an example of the login page similar to what we want to achieve in this article here: https://www.mecallapi.com/login/

https://www.mecallapi.com/login/

_https://www.mecallapi.com/login/

Software Installation

It's only required a Text Editor/IDE (VS Code, Notepad, etc.) and a web browser (Chrome, Firefox, Edge, etc.) to do this tutorial!

Let's Code! (HTML and CSS)

HTML documents are designed to be displayed in a web browser. There are more than a hundred of HTML elements you can choose to create an HTML file. Let's start from creating index.html. I will explain a bit about what is included:

Bootstrap 5, a framework to create a responsive web pages (line 9 and 36).

login.css, an extra CSS (Cascading Style Sheets) to style your login.html in addition from the Bootstrap 5 (line 12).

A login form with the inputs for username and password and a button (line 15–32).

Sweetalert, a JavaScript library for easily creating nice popups (line 35).

login.js, JavaScript file using in login.html to call a login API (line 34).

My App My App - Login Email address Password Login Technology vector created by freepik - www.freepik.com

Create login.css

html, body { height: 100%; } body { padding-top: 5rem; background-color: #f5f5f5; } .form-signin { max-width: 25rem; margin: auto; }

Open login.html on a web browser to see the result:

login.html on a web browser

login.html on a web browser

Create index.html to show the information of the currently logged user. Therefore, at the end of this tutorial, this page will be only accessible when logged in.

My App My App ... Logout ...

Create index.css

.bg-mynav { background-color: #2c3e50; }

Open index.html on a web browser to see the result:

index.html on a web browser

index.html on a web browser

Login with API (JavaScript)

Create login.js to call an API for login based on JWT (JSON Web Token)Standard provided by MeCallAPI.comAPI URL: https://www.mecallapi.com/api/loginMethod: POST Sample body (JSON):

{ "username": "[email protected]", "password": "mecallapi" }

Sample Response (JSON):

{ "status": "ok", "message": "Logged in", "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cC..." }

The accessToken from the response represents the authorization of a user. Therefore, we will check whether the user is logged in by this accessToken.

In the JavaScript (line 1–4), we will first get the jwt item from localStorage (The localStorage allow to save key/value pairs in a web browser). If jwt has value which means that a user is logged in, the web browser will load index.html.

We create a function login which will execute when user click Login button in login.html. In this function, we use XMLHttpRequest to request an API for retrieving response in JSON. If the response status is ok (meaning that login successful), we will save the accessToken value to jwt in localStorage and show the popup (Sweetalert). Once the user click ok in the popup, the web browser will load index.html.

var jwt = localStorage.getItem("jwt"); if (jwt != null) { window.location.href = "./index.html"; } function login() { const username = document.getElementById("username").value; const password = document.getElementById("password").value; const xhttp = new XMLHttpRequest(); xhttp.open("POST", "https://www.mecallapi.com/api/login"); xhttp.setRequestHeader("Content-Type", "application/json;"); xhttp.send( JSON.stringify({ username: username, password: password, }) ); xhttp.onreadystatechange = function () { if (this.readyState == 4) { const objects = JSON.parse(this.responseText); console.log(objects); if (objects["status"] == "ok") { localStorage.setItem("jwt", objects["accessToken"]); Swal.fire({ text: objects["message"], icon: "success", confirmButtonText: "OK", }).then((result) => { if (result.isConfirmed) { window.location.href = "./index.html"; } }); } else { Swal.fire({ text: objects["message"], icon: "error", confirmButtonText: "OK", }); } } }; return false; }

Open login.html on a web browser, therefore input:username: [email protected]: mecallapiWe can use other usernames from MeCallAPI.com while password remains the same.Click Login button and you will see this result:

image

User information with API (JavaScript)

Create index.js to request the API for retrieving the information of the currently logged user.API URL: https://www.mecallapi.com/api/auth/userMethod: GETThe API request header needs to have the value of access token of the user as Authorization (Bearer) to response back with that user information.

In the JavaScript (line 1–4), we check if jwt item from localStorage has value or not. If it is not, the web browser will load login.html.

We create a loadUser function to retrieve and display the currently logged user information in index.html. Basically, we use XMLHttpRequest to call the API with jwt in the Authorization header (Bearer). Note that this function will be called when loading index.html.

We create a logout function to remove jwt from localStorage then load login.html on the web browser. This will be called when click Logout button.

var jwt = localStorage.getItem("jwt"); if (jwt == null) { window.location.href = "./login.html"; } function loadUser() { const xhttp = new XMLHttpRequest(); xhttp.open("GET", "https://www.mecallapi.com/api/auth/user"); xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhttp.setRequestHeader("Authorization", "Bearer " + jwt); xhttp.send(); xhttp.onreadystatechange = function () { if (this.readyState == 4) { const objects = JSON.parse(this.responseText); if (objects["status"] == "ok") { const user = objects["user"]; document.getElementById("fname").innerHTML = user["fname"]; document.getElementById("avatar").src = user["avatar"]; document.getElementById("username").innerHTML = user["username"]; } } }; } loadUser(); function logout() { localStorage.removeItem("jwt"); window.location.href = "./login.html"; }

The result:

index.html after logged in

index.html after logged in

Conclusion

That's it for building a basic login page using just HTML, CSS, JavaScript and an API. As now most of the applications are driven with API, therefore I will try to cover more about the API in the upcoming articles. Stay tuned. :D



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3